package vn.fpt.snake.unitTest;

import static org.junit.Assert.*;
import java.util.List;
import java.awt.Image;
import java.util.ArrayList;

import vn.fpt.snake.commons.Constants;
import vn.fpt.snake.commons.Resource;
import vn.fpt.snake.controller.FruitControllerImpl;
import vn.fpt.snake.controller.GameControllerImpl;
import org.hamcrest.core.IsInstanceOf;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import vn.fpt.snake.entity.AbstractFruit;
import vn.fpt.snake.entity.Apple;
import vn.fpt.snake.entity.Chili;
import vn.fpt.snake.entity.Settings;
import vn.fpt.snake.entity.Snake;
import vn.fpt.snake.entity.Watermelon;
import vn.fpt.snake.ui.*;
import vn.fpt.snake.entity.*;

public class FruitControllerImplTest {
	Snake snake;
	Apple apple;
	Settings setting;
	int with;
	int height;
	FruitControllerImpl fruitController;
	private Image appleIcon;
	List<AbstractFruit> listFruits;
	GameControllerImpl gameController;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
		gameController = new GameControllerImpl();
		listFruits = new ArrayList<AbstractFruit>();
		fruitController = new FruitControllerImpl();
		setting = gameController.getSettings();
		with = setting.getWidth();
		height = setting.getHeight();
		/* New snake */
		snake = new Snake(1000);
		for (int x = 10; x < 20; x++) {
			snake.setX(x, x);
		}
		for (int y = 10; y < 20; y++) {
			snake.setY(y, y);
		}

		/* new Apple */
		apple = new Apple();
		apple.setX(10);
		apple.setY(10);
	}

	@After
	public void tearDown() throws Exception {
		listFruits.clear();
		gameController = null;
		fruitController = null;
		setting = null;
		snake = null;
		apple = null;

	}

	@Test
	public void testCreateNewApple_input_notNull() {
		/* Check apple not null */
		try{
			int appleX;
			int apppleY;
			appleIcon = gameController.getImage(Constants.APPLE_IMG);
			Apple appOutput;
			appOutput = fruitController.createNewApple();
			appleX = appOutput.getX();
			apppleY = appOutput.getY();
			assertNotNull(appOutput);
			/* Check property */
			assertFalse(appleX == with);
			assertFalse(apppleY == height);
			assertTrue(appOutput.getIcon() == appleIcon);
			assertTrue(appleX >= 0);
			assertTrue(apppleY >= 0);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			// Xu ly test neu co exception 
			// Trong test case anh ko dc ex.printStachtrace dau
		}
		
	}

	@Test
	public void testCreateChili_input_Notnull() {
		Chili chili;
		try {
			chili = fruitController.createNewChili();
			assertNotNull(chili);
			assertTrue(chili.getX() == -1);
			assertTrue(chili.getY() == -1);
			assertTrue(chili.getIcon() == null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Test
	public void testCreateWaterMelon_inputnull_notnull() {
		Watermelon waterMelon;
		try {
			waterMelon = fruitController.createNewWatermelon();
			assertNotNull(waterMelon);
			assertTrue(waterMelon.getX() == -1);
			assertTrue(waterMelon.getY() == -1);
			assertTrue(waterMelon.getIcon() == null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Test
	public void testlocateChili__inputListAbstractFruit_changePoint() {
		try {
			Chili chili = fruitController.createNewChili();
			listFruits.add(chili);
			fruitController.locateChili(listFruits);
			AbstractFruit fruitsOutput = (AbstractFruit) listFruits.get(0);
			assertTrue(fruitsOutput instanceof Chili);
			assertTrue(fruitsOutput.getX() > 0);
			assertTrue(fruitsOutput.getY() > 0);
			assertTrue(fruitsOutput.getX() <= with);
			assertTrue(fruitsOutput.getY() <= height);
			assertTrue(fruitsOutput.getIcon() == gameController.getImage(Constants.CHILI_IMG) );
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//sua lai
		}

	}

	@Test
	public void testLocateWaterMelon_inputListAbsrtactFruit_ChangePoint() {
		try {
			Watermelon waterMelon = fruitController.createNewWatermelon();
			listFruits.add(waterMelon);
			fruitController.locateWatermelon(listFruits);
			AbstractFruit fruitsOutput = listFruits.get(0);
			assertTrue(waterMelon instanceof Watermelon);
			assertTrue(fruitsOutput.getX() > 0);
			assertTrue(fruitsOutput.getY() > 0);
			assertTrue(fruitsOutput.getX() <= with);
			assertTrue(fruitsOutput.getY() <= height);
			assertTrue(fruitsOutput.getIcon() == gameController.getImage(Constants.WATERMELON_IMG));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//sua lai
		}

	}

}
